home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_make.idb / usr / freeware / info / make.info-4.z / make.info-4 (.txt)
GNU Info File  |  1998-05-21  |  48KB  |  907 lines

  1. This is Info file make.info, produced by Makeinfo version 1.67 from the
  2. input file make.texinfo.
  3. INFO-DIR-SECTION The GNU make utility
  4. START-INFO-DIR-ENTRY
  5. * GNU make: (make.info).           The GNU make utility.
  6. END-INFO-DIR-ENTRY
  7.    This file documents the GNU Make utility, which determines
  8. automatically which pieces of a large program need to be recompiled,
  9. and issues the commands to recompile them.
  10.    This is Edition 0.51, last updated 26 Aug 1997, of `The GNU Make
  11. Manual', for `make', Version 3.76 Beta.
  12.    Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97     Free
  13. Software Foundation, Inc.
  14.    Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17.    Permission is granted to copy and distribute modified versions of
  18. this manual under the conditions for verbatim copying, provided that
  19. the entire resulting derived work is distributed under the terms of a
  20. permission notice identical to this one.
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions, except that this permission notice may be stated in a
  24. translation approved by the Free Software Foundation.
  25. File: make.info,  Node: Setting,  Next: Appending,  Prev: Values,  Up: Using Variables
  26. Setting Variables
  27. =================
  28.    To set a variable from the makefile, write a line starting with the
  29. variable name followed by `=' or `:='.  Whatever follows the `=' or
  30. `:=' on the line becomes the value.  For example,
  31.      objects = main.o foo.o bar.o utils.o
  32. defines a variable named `objects'.  Whitespace around the variable
  33. name and immediately after the `=' is ignored.
  34.    Variables defined with `=' are "recursively expanded" variables.
  35. Variables defined with `:=' are "simply expanded" variables; these
  36. definitions can contain variable references which will be expanded
  37. before the definition is made.  *Note The Two Flavors of Variables:
  38. Flavors.
  39.    The variable name may contain function and variable references, which
  40. are expanded when the line is read to find the actual variable name to
  41.    There is no limit on the length of the value of a variable except the
  42. amount of swapping space on the computer.  When a variable definition is
  43. long, it is a good idea to break it into several lines by inserting
  44. backslash-newline at convenient places in the definition.  This will not
  45. affect the functioning of `make', but it will make the makefile easier
  46. to read.
  47.    Most variable names are considered to have the empty string as a
  48. value if you have never set them.  Several variables have built-in
  49. initial values that are not empty, but you can set them in the usual
  50. ways (*note Variables Used by Implicit Rules: Implicit Variables.).
  51. Several special variables are set automatically to a new value for each
  52. rule; these are called the "automatic" variables (*note Automatic
  53. Variables: Automatic.).
  54. File: make.info,  Node: Appending,  Next: Override Directive,  Prev: Setting,  Up: Using Variables
  55. Appending More Text to Variables
  56. ================================
  57.    Often it is useful to add more text to the value of a variable
  58. already defined.  You do this with a line containing `+=', like this:
  59.      objects += another.o
  60. This takes the value of the variable `objects', and adds the text
  61. `another.o' to it (preceded by a single space).  Thus:
  62.      objects = main.o foo.o bar.o utils.o
  63.      objects += another.o
  64. sets `objects' to `main.o foo.o bar.o utils.o another.o'.
  65.    Using `+=' is similar to:
  66.      objects = main.o foo.o bar.o utils.o
  67.      objects := $(objects) another.o
  68. but differs in ways that become important when you use more complex
  69. values.
  70.    When the variable in question has not been defined before, `+=' acts
  71. just like normal `=': it defines a recursively-expanded variable.
  72. However, when there *is* a previous definition, exactly what `+=' does
  73. depends on what flavor of variable you defined originally.  *Note The
  74. Two Flavors of Variables: Flavors, for an explanation of the two
  75. flavors of variables.
  76.    When you add to a variable's value with `+=', `make' acts
  77. essentially as if you had included the extra text in the initial
  78. definition of the variable.  If you defined it first with `:=', making
  79. it a simply-expanded variable, `+=' adds to that simply-expanded
  80. definition, and expands the new text before appending it to the old
  81. value just as `:=' does (*note Setting Variables: Setting., for a full
  82. explanation of `:=').  In fact,
  83.      variable := value
  84.      variable += more
  85. is exactly equivalent to:
  86.      variable := value
  87.      variable := $(variable) more
  88.    On the other hand, when you use `+=' with a variable that you defined
  89. first to be recursively-expanded using plain `=', `make' does something
  90. a bit different.  Recall that when you define a recursively-expanded
  91. variable, `make' does not expand the value you set for variable and
  92. function references immediately.  Instead it stores the text verbatim,
  93. and saves these variable and function references to be expanded later,
  94. when you refer to the new variable (*note The Two Flavors of Variables:
  95. Flavors.).  When you use `+=' on a recursively-expanded variable, it is
  96. this unexpanded text to which `make' appends the new text you specify.
  97.      variable = value
  98.      variable += more
  99. is roughly equivalent to:
  100.      temp = value
  101.      variable = $(temp) more
  102. except that of course it never defines a variable called `temp'.  The
  103. importance of this comes when the variable's old value contains
  104. variable references.  Take this common example:
  105.      CFLAGS = $(includes) -O
  106.      ...
  107.      CFLAGS += -pg # enable profiling
  108. The first line defines the `CFLAGS' variable with a reference to another
  109. variable, `includes'.  (`CFLAGS' is used by the rules for C
  110. compilation; *note Catalogue of Implicit Rules: Catalogue of Rules..)
  111. Using `=' for the definition makes `CFLAGS' a recursively-expanded
  112. variable, meaning `$(includes) -O' is *not* expanded when `make'
  113. processes the definition of `CFLAGS'.  Thus, `includes' need not be
  114. defined yet for its value to take effect.  It only has to be defined
  115. before any reference to `CFLAGS'.  If we tried to append to the value
  116. of `CFLAGS' without using `+=', we might do it like this:
  117.      CFLAGS := $(CFLAGS) -pg # enable profiling
  118. This is pretty close, but not quite what we want.  Using `:=' redefines
  119. `CFLAGS' as a simply-expanded variable; this means `make' expands the
  120. text `$(CFLAGS) -pg' before setting the variable.  If `includes' is not
  121. yet defined, we get ` -O -pg', and a later definition of `includes'
  122. will have no effect.  Conversely, by using `+=' we set `CFLAGS' to the
  123. *unexpanded* value `$(includes) -O -pg'.  Thus we preserve the
  124. reference to `includes', so if that variable gets defined at any later
  125. point, a reference like `$(CFLAGS)' still uses its value.
  126. File: make.info,  Node: Override Directive,  Next: Defining,  Prev: Appending,  Up: Using Variables
  127. The `override' Directive
  128. ========================
  129.    If a variable has been set with a command argument (*note Overriding
  130. Variables: Overriding.), then ordinary assignments in the makefile are
  131. ignored.  If you want to set the variable in the makefile even though
  132. it was set with a command argument, you can use an `override'
  133. directive, which is a line that looks like this:
  134.      override VARIABLE = VALUE
  135.      override VARIABLE := VALUE
  136.    To append more text to a variable defined on the command line, use:
  137.      override VARIABLE += MORE TEXT
  138. *Note Appending More Text to Variables: Appending.
  139.    The `override' directive was not invented for escalation in the war
  140. between makefiles and command arguments.  It was invented so you can
  141. alter and add to values that the user specifies with command arguments.
  142.    For example, suppose you always want the `-g' switch when you run the
  143. C compiler, but you would like to allow the user to specify the other
  144. switches with a command argument just as usual.  You could use this
  145. `override' directive:
  146.      override CFLAGS += -g
  147.    You can also use `override' directives with `define' directives.
  148. This is done as you might expect:
  149.      override define foo
  150.      bar
  151.      endef
  152. *Note Defining Variables Verbatim: Defining.
  153. File: make.info,  Node: Defining,  Next: Environment,  Prev: Override Directive,  Up: Using Variables
  154. Defining Variables Verbatim
  155. ===========================
  156. Another way to set the value of a variable is to use the `define'
  157. directive.  This directive has an unusual syntax which allows newline
  158. characters to be included in the value, which is convenient for defining
  159. canned sequences of commands (*note Defining Canned Command Sequences:
  160. Sequences.).
  161.    The `define' directive is followed on the same line by the name of
  162. the variable and nothing more.  The value to give the variable appears
  163. on the following lines.  The end of the value is marked by a line
  164. containing just the word `endef'.  Aside from this difference in
  165. syntax, `define' works just like `=': it creates a recursively-expanded
  166. variable (*note The Two Flavors of Variables: Flavors.).  The variable
  167. name may contain function and variable references, which are expanded
  168. when the directive is read to find the actual variable name to use.
  169.      define two-lines
  170.      echo foo
  171.      echo $(bar)
  172.      endef
  173.    The value in an ordinary assignment cannot contain a newline; but the
  174. newlines that separate the lines of the value in a `define' become part
  175. of the variable's value (except for the final newline which precedes
  176. the `endef' and is not considered part of the value).
  177.    The previous example is functionally equivalent to this:
  178.      two-lines = echo foo; echo $(bar)
  179. since two commands separated by semicolon behave much like two separate
  180. shell commands.  However, note that using two separate lines means
  181. `make' will invoke the shell twice, running an independent subshell for
  182. each line.  *Note Command Execution: Execution.
  183.    If you want variable definitions made with `define' to take
  184. precedence over command-line variable definitions, you can use the
  185. `override' directive together with `define':
  186.      override define two-lines
  187.      foo
  188.      $(bar)
  189.      endef
  190. *Note The `override' Directive: Override Directive.
  191. File: make.info,  Node: Environment,  Prev: Defining,  Up: Using Variables
  192. Variables from the Environment
  193. ==============================
  194.    Variables in `make' can come from the environment in which `make' is
  195. run.  Every environment variable that `make' sees when it starts up is
  196. transformed into a `make' variable with the same name and value.  But
  197. an explicit assignment in the makefile, or with a command argument,
  198. overrides the environment.  (If the `-e' flag is specified, then values
  199. from the environment override assignments in the makefile.  *Note
  200. Summary of Options: Options Summary.  But this is not recommended
  201. practice.)
  202.    Thus, by setting the variable `CFLAGS' in your environment, you can
  203. cause all C compilations in most makefiles to use the compiler switches
  204. you prefer.  This is safe for variables with standard or conventional
  205. meanings because you know that no makefile will use them for other
  206. things.  (But this is not totally reliable; some makefiles set `CFLAGS'
  207. explicitly and therefore are not affected by the value in the
  208. environment.)
  209.    When `make' is invoked recursively, variables defined in the outer
  210. invocation can be passed to inner invocations through the environment
  211. (*note Recursive Use of `make': Recursion.).  By default, only
  212. variables that came from the environment or the command line are passed
  213. to recursive invocations.  You can use the `export' directive to pass
  214. other variables.  *Note Communicating Variables to a Sub-`make':
  215. Variables/Recursion, for full details.
  216.    Other use of variables from the environment is not recommended.  It
  217. is not wise for makefiles to depend for their functioning on
  218. environment variables set up outside their control, since this would
  219. cause different users to get different results from the same makefile.
  220. This is against the whole purpose of most makefiles.
  221.    Such problems would be especially likely with the variable `SHELL',
  222. which is normally present in the environment to specify the user's
  223. choice of interactive shell.  It would be very undesirable for this
  224. choice to affect `make'.  So `make' ignores the environment value of
  225. `SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not
  226. set.  *Note Special handling of SHELL on MS-DOS: Execution.)
  227. File: make.info,  Node: Conditionals,  Next: Functions,  Prev: Using Variables,  Up: Top
  228. Conditional Parts of Makefiles
  229. ******************************
  230.    A "conditional" causes part of a makefile to be obeyed or ignored
  231. depending on the values of variables.  Conditionals can compare the
  232. value of one variable to another, or the value of a variable to a
  233. constant string.  Conditionals control what `make' actually "sees" in
  234. the makefile, so they *cannot* be used to control shell commands at the
  235. time of execution.
  236. * Menu:
  237. * Conditional Example::         Example of a conditional
  238. * Conditional Syntax::          The syntax of conditionals.
  239. * Testing Flags::               Conditionals that test flags.
  240. File: make.info,  Node: Conditional Example,  Next: Conditional Syntax,  Up: Conditionals
  241. Example of a Conditional
  242. ========================
  243.    The following example of a conditional tells `make' to use one set
  244. of libraries if the `CC' variable is `gcc', and a different set of
  245. libraries otherwise.  It works by controlling which of two command
  246. lines will be used as the command for a rule.  The result is that
  247. `CC=gcc' as an argument to `make' changes not only which compiler is
  248. used but also which libraries are linked.
  249.      libs_for_gcc = -lgnu
  250.      normal_libs =
  251.      
  252.      foo: $(objects)
  253.      ifeq ($(CC),gcc)
  254.              $(CC) -o foo $(objects) $(libs_for_gcc)
  255.      else
  256.              $(CC) -o foo $(objects) $(normal_libs)
  257.      endif
  258.    This conditional uses three directives: one `ifeq', one `else' and
  259. one `endif'.
  260.    The `ifeq' directive begins the conditional, and specifies the
  261. condition.  It contains two arguments, separated by a comma and
  262. surrounded by parentheses.  Variable substitution is performed on both
  263. arguments and then they are compared.  The lines of the makefile
  264. following the `ifeq' are obeyed if the two arguments match; otherwise
  265. they are ignored.
  266.    The `else' directive causes the following lines to be obeyed if the
  267. previous conditional failed.  In the example above, this means that the
  268. second alternative linking command is used whenever the first
  269. alternative is not used.  It is optional to have an `else' in a
  270. conditional.
  271.    The `endif' directive ends the conditional.  Every conditional must
  272. end with an `endif'.  Unconditional makefile text follows.
  273.    As this example illustrates, conditionals work at the textual level:
  274. the lines of the conditional are treated as part of the makefile, or
  275. ignored, according to the condition.  This is why the larger syntactic
  276. units of the makefile, such as rules, may cross the beginning or the
  277. end of the conditional.
  278.    When the variable `CC' has the value `gcc', the above example has
  279. this effect:
  280.      foo: $(objects)
  281.              $(CC) -o foo $(objects) $(libs_for_gcc)
  282. When the variable `CC' has any other value, the effect is this:
  283.      foo: $(objects)
  284.              $(CC) -o foo $(objects) $(normal_libs)
  285.    Equivalent results can be obtained in another way by
  286. conditionalizing a variable assignment and then using the variable
  287. unconditionally:
  288.      libs_for_gcc = -lgnu
  289.      normal_libs =
  290.      
  291.      ifeq ($(CC),gcc)
  292.        libs=$(libs_for_gcc)
  293.      else
  294.        libs=$(normal_libs)
  295.      endif
  296.      
  297.      foo: $(objects)
  298.              $(CC) -o foo $(objects) $(libs)
  299. File: make.info,  Node: Conditional Syntax,  Next: Testing Flags,  Prev: Conditional Example,  Up: Conditionals
  300. Syntax of Conditionals
  301. ======================
  302.    The syntax of a simple conditional with no `else' is as follows:
  303.      CONDITIONAL-DIRECTIVE
  304.      TEXT-IF-TRUE
  305.      endif
  306. The TEXT-IF-TRUE may be any lines of text, to be considered as part of
  307. the makefile if the condition is true.  If the condition is false, no
  308. text is used instead.
  309.    The syntax of a complex conditional is as follows:
  310.      CONDITIONAL-DIRECTIVE
  311.      TEXT-IF-TRUE
  312.      else
  313.      TEXT-IF-FALSE
  314.      endif
  315. If the condition is true, TEXT-IF-TRUE is used; otherwise,
  316. TEXT-IF-FALSE is used instead.  The TEXT-IF-FALSE can be any number of
  317. lines of text.
  318.    The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
  319. conditional is simple or complex.  There are four different directives
  320. that test different conditions.  Here is a table of them:
  321. `ifeq (ARG1, ARG2)'
  322. `ifeq 'ARG1' 'ARG2''
  323. `ifeq "ARG1" "ARG2"'
  324. `ifeq "ARG1" 'ARG2''
  325. `ifeq 'ARG1' "ARG2"'
  326.      Expand all variable references in ARG1 and ARG2 and compare them.
  327.      If they are identical, the TEXT-IF-TRUE is effective; otherwise,
  328.      the TEXT-IF-FALSE, if any, is effective.
  329.      Often you want to test if a variable has a non-empty value.  When
  330.      the value results from complex expansions of variables and
  331.      functions, expansions you would consider empty may actually
  332.      contain whitespace characters and thus are not seen as empty.
  333.      However, you can use the `strip' function (*note Text
  334.      Functions::.) to avoid interpreting whitespace as a non-empty
  335.      value.  For example:
  336.           ifeq ($(strip $(foo)),)
  337.           TEXT-IF-EMPTY
  338.           endif
  339.      will evaluate TEXT-IF-EMPTY even if the expansion of `$(foo)'
  340.      contains whitespace characters.
  341. `ifneq (ARG1, ARG2)'
  342. `ifneq 'ARG1' 'ARG2''
  343. `ifneq "ARG1" "ARG2"'
  344. `ifneq "ARG1" 'ARG2''
  345. `ifneq 'ARG1' "ARG2"'
  346.      Expand all variable references in ARG1 and ARG2 and compare them.
  347.      If they are different, the TEXT-IF-TRUE is effective; otherwise,
  348.      the TEXT-IF-FALSE, if any, is effective.
  349. `ifdef VARIABLE-NAME'
  350.      If the variable VARIABLE-NAME has a non-empty value, the
  351.      TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any,
  352.      is effective.  Variables that have never been defined have an
  353.      empty value.
  354.      Note that `ifdef' only tests whether a variable has a value.  It
  355.      does not expand the variable to see if that value is nonempty.
  356.      Consequently, tests using `ifdef' return true for all definitions
  357.      except those like `foo ='.  To test for an empty value, use
  358.      `ifeq ($(foo),)'.  For example,
  359.           bar =
  360.           foo = $(bar)
  361.           ifdef foo
  362.           frobozz = yes
  363.           else
  364.           frobozz = no
  365.           endif
  366.      sets `frobozz' to `yes', while:
  367.           foo =
  368.           ifdef foo
  369.           frobozz = yes
  370.           else
  371.           frobozz = no
  372.           endif
  373.      sets `frobozz' to `no'.
  374. `ifndef VARIABLE-NAME'
  375.      If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
  376.      is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
  377.    Extra spaces are allowed and ignored at the beginning of the
  378. conditional directive line, but a tab is not allowed.  (If the line
  379. begins with a tab, it will be considered a command for a rule.)  Aside
  380. from this, extra spaces or tabs may be inserted with no effect anywhere
  381. except within the directive name or within an argument.  A comment
  382. starting with `#' may appear at the end of the line.
  383.    The other two directives that play a part in a conditional are `else'
  384. and `endif'.  Each of these directives is written as one word, with no
  385. arguments.  Extra spaces are allowed and ignored at the beginning of the
  386. line, and spaces or tabs at the end.  A comment starting with `#' may
  387. appear at the end of the line.
  388.    Conditionals affect which lines of the makefile `make' uses.  If the
  389. condition is true, `make' reads the lines of the TEXT-IF-TRUE as part
  390. of the makefile; if the condition is false, `make' ignores those lines
  391. completely.  It follows that syntactic units of the makefile, such as
  392. rules, may safely be split across the beginning or the end of the
  393. conditional.
  394.    `make' evaluates conditionals when it reads a makefile.
  395. Consequently, you cannot use automatic variables in the tests of
  396. conditionals because they are not defined until commands are run (*note
  397. Automatic Variables: Automatic.).
  398.    To prevent intolerable confusion, it is not permitted to start a
  399. conditional in one makefile and end it in another.  However, you may
  400. write an `include' directive within a conditional, provided you do not
  401. attempt to terminate the conditional inside the included file.
  402. File: make.info,  Node: Testing Flags,  Prev: Conditional Syntax,  Up: Conditionals
  403. Conditionals that Test Flags
  404. ============================
  405.    You can write a conditional that tests `make' command flags such as
  406. `-t' by using the variable `MAKEFLAGS' together with the `findstring'
  407. function (*note Functions for String Substitution and Analysis: Text
  408. Functions.).  This is useful when `touch' is not enough to make a file
  409. appear up to date.
  410.    The `findstring' function determines whether one string appears as a
  411. substring of another.  If you want to test for the `-t' flag, use `t'
  412. as the first string and the value of `MAKEFLAGS' as the other.
  413.    For example, here is how to arrange to use `ranlib -t' to finish
  414. marking an archive file up to date:
  415.      archive.a: ...
  416.      ifneq (,$(findstring t,$(MAKEFLAGS)))
  417.              +touch archive.a
  418.              +ranlib -t archive.a
  419.      else
  420.              ranlib archive.a
  421.      endif
  422. The `+' prefix marks those command lines as "recursive" so that they
  423. will be executed despite use of the `-t' flag.  *Note Recursive Use of
  424. `make': Recursion.
  425. File: make.info,  Node: Functions,  Next: Running,  Prev: Conditionals,  Up: Top
  426. Functions for Transforming Text
  427. *******************************
  428.    "Functions" allow you to do text processing in the makefile to
  429. compute the files to operate on or the commands to use.  You use a
  430. function in a "function call", where you give the name of the function
  431. and some text (the "arguments") for the function to operate on.  The
  432. result of the function's processing is substituted into the makefile at
  433. the point of the call, just as a variable might be substituted.
  434. * Menu:
  435. * Syntax of Functions::         How to write a function call.
  436. * Text Functions::              General-purpose text manipulation functions.
  437. * File Name Functions::         Functions for manipulating file names.
  438. * Foreach Function::            Repeat some text with controlled variation.
  439. * Origin Function::             Find where a variable got its value.
  440. * Shell Function::              Substitute the output of a shell command.
  441. File: make.info,  Node: Syntax of Functions,  Next: Text Functions,  Up: Functions
  442. Function Call Syntax
  443. ====================
  444.    A function call resembles a variable reference.  It looks like this:
  445.      $(FUNCTION ARGUMENTS)
  446. or like this:
  447.      ${FUNCTION ARGUMENTS}
  448.    Here FUNCTION is a function name; one of a short list of names that
  449. are part of `make'.  There is no provision for defining new functions.
  450.    The ARGUMENTS are the arguments of the function.  They are separated
  451. from the function name by one or more spaces or tabs, and if there is
  452. more than one argument, then they are separated by commas.  Such
  453. whitespace and commas are not part of an argument's value.  The
  454. delimiters which you use to surround the function call, whether
  455. parentheses or braces, can appear in an argument only in matching pairs;
  456. the other kind of delimiters may appear singly.  If the arguments
  457. themselves contain other function calls or variable references, it is
  458. wisest to use the same kind of delimiters for all the references; write
  459. `$(subst a,b,$(x))', not `$(subst a,b,${x})'.  This is because it is
  460. clearer, and because only one type of delimiter is matched to find the
  461. end of the reference.
  462.    The text written for each argument is processed by substitution of
  463. variables and function calls to produce the argument value, which is
  464. the text on which the function acts.  The substitution is done in the
  465. order in which the arguments appear.
  466.    Commas and unmatched parentheses or braces cannot appear in the text
  467. of an argument as written; leading spaces cannot appear in the text of
  468. the first argument as written.  These characters can be put into the
  469. argument value by variable substitution.  First define variables
  470. `comma' and `space' whose values are isolated comma and space
  471. characters, then substitute these variables where such characters are
  472. wanted, like this:
  473.      comma:= ,
  474.      empty:=
  475.      space:= $(empty) $(empty)
  476.      foo:= a b c
  477.      bar:= $(subst $(space),$(comma),$(foo))
  478.      # bar is now `a,b,c'.
  479. Here the `subst' function replaces each space with a comma, through the
  480. value of `foo', and substitutes the result.
  481. File: make.info,  Node: Text Functions,  Next: File Name Functions,  Prev: Syntax of Functions,  Up: Functions
  482. Functions for String Substitution and Analysis
  483. ==============================================
  484.    Here are some functions that operate on strings:
  485. `$(subst FROM,TO,TEXT)'
  486.      Performs a textual replacement on the text TEXT: each occurrence
  487.      of FROM is replaced by TO.  The result is substituted for the
  488.      function call.  For example,
  489.           $(subst ee,EE,feet on the street)
  490.      substitutes the string `fEEt on the strEEt'.
  491. `$(patsubst PATTERN,REPLACEMENT,TEXT)'
  492.      Finds whitespace-separated words in TEXT that match PATTERN and
  493.      replaces them with REPLACEMENT.  Here PATTERN may contain a `%'
  494.      which acts as a wildcard, matching any number of any characters
  495.      within a word.  If REPLACEMENT also contains a `%', the `%' is
  496.      replaced by the text that matched the `%' in PATTERN.
  497.      `%' characters in `patsubst' function invocations can be quoted
  498.      with preceding backslashes (`\').  Backslashes that would
  499.      otherwise quote `%' characters can be quoted with more backslashes.
  500.      Backslashes that quote `%' characters or other backslashes are
  501.      removed from the pattern before it is compared file names or has a
  502.      stem substituted into it.  Backslashes that are not in danger of
  503.      quoting `%' characters go unmolested.  For example, the pattern
  504.      `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
  505.      `%' character, and `pattern\\' following it.  The final two
  506.      backslashes are left alone because they cannot affect any `%'
  507.      character.
  508.      Whitespace between words is folded into single space characters;
  509.      leading and trailing whitespace is discarded.
  510.      For example,
  511.           $(patsubst %.c,%.o,x.c.c bar.c)
  512.      produces the value `x.c.o bar.o'.
  513.      Substitution references (*note Substitution References:
  514.      Substitution Refs.) are a simpler way to get the effect of the
  515.      `patsubst' function:
  516.           $(VAR:PATTERN=REPLACEMENT)
  517.      is equivalent to
  518.           $(patsubst PATTERN,REPLACEMENT,$(VAR))
  519.      The second shorthand simplifies one of the most common uses of
  520.      `patsubst': replacing the suffix at the end of file names.
  521.           $(VAR:SUFFIX=REPLACEMENT)
  522.      is equivalent to
  523.           $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))
  524.      For example, you might have a list of object files:
  525.           objects = foo.o bar.o baz.o
  526.      To get the list of corresponding source files, you could simply
  527.      write:
  528.           $(objects:.o=.c)
  529.      instead of using the general form:
  530.           $(patsubst %.o,%.c,$(objects))
  531. `$(strip STRING)'
  532.      Removes leading and trailing whitespace from STRING and replaces
  533.      each internal sequence of one or more whitespace characters with a
  534.      single space.  Thus, `$(strip a b  c )' results in `a b c'.
  535.      The function `strip' can be very useful when used in conjunction
  536.      with conditionals.  When comparing something with the empty string
  537.      `' using `ifeq' or `ifneq', you usually want a string of just
  538.      whitespace to match the empty string (*note Conditionals::.).
  539.      Thus, the following may fail to have the desired results:
  540.           .PHONY: all
  541.           ifneq   "$(needs_made)" ""
  542.           all: $(needs_made)
  543.           else
  544.           all:;@echo 'Nothing to make!'
  545.           endif
  546.      Replacing the variable reference `$(needs_made)' with the function
  547.      call `$(strip $(needs_made))' in the `ifneq' directive would make
  548.      it more robust.
  549. `$(findstring FIND,IN)'
  550.      Searches IN for an occurrence of FIND.  If it occurs, the value is
  551.      FIND; otherwise, the value is empty.  You can use this function in
  552.      a conditional to test for the presence of a specific substring in
  553.      a given string.  Thus, the two examples,
  554.           $(findstring a,a b c)
  555.           $(findstring a,b c)
  556.      produce the values `a' and `' (the empty string), respectively.
  557.      *Note Testing Flags::, for a practical application of `findstring'.
  558. `$(filter PATTERN...,TEXT)'
  559.      Removes all whitespace-separated words in TEXT that do *not* match
  560.      any of the PATTERN words, returning only matching words.  The
  561.      patterns are written using `%', just like the patterns used in the
  562.      `patsubst' function above.
  563.      The `filter' function can be used to separate out different types
  564.      of strings (such as file names) in a variable.  For example:
  565.           sources := foo.c bar.c baz.s ugh.h
  566.           foo: $(sources)
  567.                   cc $(filter %.c %.s,$(sources)) -o foo
  568.      says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
  569.      but only `foo.c', `bar.c' and `baz.s' should be specified in the
  570.      command to the compiler.
  571. `$(filter-out PATTERN...,TEXT)'
  572.      Removes all whitespace-separated words in TEXT that *do* match the
  573.      PATTERN words, returning only the words that *do not* match.  This
  574.      is the exact opposite of the `filter' function.
  575.      For example, given:
  576.           objects=main1.o foo.o main2.o bar.o
  577.           mains=main1.o main2.o
  578.      the following generates a list which contains all the object files
  579.      not in `mains':
  580.           $(filter-out $(mains),$(objects))
  581. `$(sort LIST)'
  582.      Sorts the words of LIST in lexical order, removing duplicate
  583.      words.  The output is a list of words separated by single spaces.
  584.      Thus,
  585.           $(sort foo bar lose)
  586.      returns the value `bar foo lose'.
  587.      Incidentally, since `sort' removes duplicate words, you can use it
  588.      for this purpose even if you don't care about the sort order.
  589.    Here is a realistic example of the use of `subst' and `patsubst'.
  590. Suppose that a makefile uses the `VPATH' variable to specify a list of
  591. directories that `make' should search for dependency files (*note
  592. `VPATH' Search Path for All Dependencies: General Search.).  This
  593. example shows how to tell the C compiler to search for header files in
  594. the same list of directories.
  595.    The value of `VPATH' is a list of directories separated by colons,
  596. such as `src:../headers'.  First, the `subst' function is used to
  597. change the colons to spaces:
  598.      $(subst :, ,$(VPATH))
  599. This produces `src ../headers'.  Then `patsubst' is used to turn each
  600. directory name into a `-I' flag.  These can be added to the value of
  601. the variable `CFLAGS', which is passed automatically to the C compiler,
  602. like this:
  603.      override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
  604. The effect is to append the text `-Isrc -I../headers' to the previously
  605. given value of `CFLAGS'.  The `override' directive is used so that the
  606. new value is assigned even if the previous value of `CFLAGS' was
  607. specified with a command argument (*note The `override' Directive:
  608. Override Directive.).
  609. File: make.info,  Node: File Name Functions,  Next: Foreach Function,  Prev: Text Functions,  Up: Functions
  610. Functions for File Names
  611. ========================
  612.    Several of the built-in expansion functions relate specifically to
  613. taking apart file names or lists of file names.
  614.    Each of the following functions performs a specific transformation
  615. on a file name.  The argument of the function is regarded as a series
  616. of file names, separated by whitespace.  (Leading and trailing
  617. whitespace is ignored.)  Each file name in the series is transformed in
  618. the same way and the results are concatenated with single spaces
  619. between them.
  620. `$(dir NAMES...)'
  621.      Extracts the directory-part of each file name in NAMES.  The
  622.      directory-part of the file name is everything up through (and
  623.      including) the last slash in it.  If the file name contains no
  624.      slash, the directory part is the string `./'.  For example,
  625.           $(dir src/foo.c hacks)
  626.      produces the result `src/ ./'.
  627. `$(notdir NAMES...)'
  628.      Extracts all but the directory-part of each file name in NAMES.
  629.      If the file name contains no slash, it is left unchanged.
  630.      Otherwise, everything through the last slash is removed from it.
  631.      A file name that ends with a slash becomes an empty string.  This
  632.      is unfortunate, because it means that the result does not always
  633.      have the same number of whitespace-separated file names as the
  634.      argument had; but we do not see any other valid alternative.
  635.      For example,
  636.           $(notdir src/foo.c hacks)
  637.      produces the result `foo.c hacks'.
  638. `$(suffix NAMES...)'
  639.      Extracts the suffix of each file name in NAMES.  If the file name
  640.      contains a period, the suffix is everything starting with the last
  641.      period.  Otherwise, the suffix is the empty string.  This
  642.      frequently means that the result will be empty when NAMES is not,
  643.      and if NAMES contains multiple file names, the result may contain
  644.      fewer file names.
  645.      For example,
  646.           $(suffix src/foo.c src-1.0/bar.c hacks)
  647.      produces the result `.c .c'.
  648. `$(basename NAMES...)'
  649.      Extracts all but the suffix of each file name in NAMES.  If the
  650.      file name contains a period, the basename is everything starting
  651.      up to (and not including) the last period.  Periods in the
  652.      directory part are ignored.  If there is no period, the basename
  653.      is the entire file name.  For example,
  654.           $(basename src/foo.c src-1.0/bar hacks)
  655.      produces the result `src/foo src-1.0/bar hacks'.
  656. `$(addsuffix SUFFIX,NAMES...)'
  657.      The argument NAMES is regarded as a series of names, separated by
  658.      whitespace; SUFFIX is used as a unit.  The value of SUFFIX is
  659.      appended to the end of each individual name and the resulting
  660.      larger names are concatenated with single spaces between them.
  661.      For example,
  662.           $(addsuffix .c,foo bar)
  663.      produces the result `foo.c bar.c'.
  664. `$(addprefix PREFIX,NAMES...)'
  665.      The argument NAMES is regarded as a series of names, separated by
  666.      whitespace; PREFIX is used as a unit.  The value of PREFIX is
  667.      prepended to the front of each individual name and the resulting
  668.      larger names are concatenated with single spaces between them.
  669.      For example,
  670.           $(addprefix src/,foo bar)
  671.      produces the result `src/foo src/bar'.
  672. `$(join LIST1,LIST2)'
  673.      Concatenates the two arguments word by word: the two first words
  674.      (one from each argument) concatenated form the first word of the
  675.      result, the two second words form the second word of the result,
  676.      and so on.  So the Nth word of the result comes from the Nth word
  677.      of each argument.  If one argument has more words that the other,
  678.      the extra words are copied unchanged into the result.
  679.      For example, `$(join a b,.c .o)' produces `a.c b.o'.
  680.      Whitespace between the words in the lists is not preserved; it is
  681.      replaced with a single space.
  682.      This function can merge the results of the `dir' and `notdir'
  683.      functions, to produce the original list of files which was given
  684.      to those two functions.
  685. `$(word N,TEXT)'
  686.      Returns the Nth word of TEXT.  The legitimate values of N start
  687.      from 1.  If N is bigger than the number of words in TEXT, the
  688.      value is empty.  For example,
  689.           $(word 2, foo bar baz)
  690.      returns `bar'.
  691. `$(wordlist S,E,TEXT)'
  692.      Returns the list of words in TEXT starting with word S and ending
  693.      with word E (inclusive).  The legitimate values of S and E start
  694.      from 1.  If S is bigger than the number of words in TEXT, the
  695.      value is empty.  If E is bigger than the number of words in TEXT,
  696.      words up to the end of TEXT are returned.  If S is greater than E,
  697.      `make' swaps them for you.  For example,
  698.           $(wordlist 2, 3, foo bar baz)
  699.      returns `bar baz'.
  700. `$(words TEXT)'
  701.      Returns the number of words in TEXT.  Thus, the last word of TEXT
  702.      is `$(word $(words TEXT),TEXT)'.
  703. `$(firstword NAMES...)'
  704.      The argument NAMES is regarded as a series of names, separated by
  705.      whitespace.  The value is the first name in the series.  The rest
  706.      of the names are ignored.
  707.      For example,
  708.           $(firstword foo bar)
  709.      produces the result `foo'.  Although `$(firstword TEXT)' is the
  710.      same as `$(word 1,TEXT)', the `firstword' function is retained for
  711.      its simplicity.
  712. `$(wildcard PATTERN)'
  713.      The argument PATTERN is a file name pattern, typically containing
  714.      wildcard characters (as in shell file name patterns).  The result
  715.      of `wildcard' is a space-separated list of the names of existing
  716.      files that match the pattern.  *Note Using Wildcard Characters in
  717.      File Names: Wildcards.
  718. File: make.info,  Node: Foreach Function,  Next: Origin Function,  Prev: File Name Functions,  Up: Functions
  719. The `foreach' Function
  720. ======================
  721.    The `foreach' function is very different from other functions.  It
  722. causes one piece of text to be used repeatedly, each time with a
  723. different substitution performed on it.  It resembles the `for' command
  724. in the shell `sh' and the `foreach' command in the C-shell `csh'.
  725.    The syntax of the `foreach' function is:
  726.      $(foreach VAR,LIST,TEXT)
  727. The first two arguments, VAR and LIST, are expanded before anything
  728. else is done; note that the last argument, TEXT, is *not* expanded at
  729. the same time.  Then for each word of the expanded value of LIST, the
  730. variable named by the expanded value of VAR is set to that word, and
  731. TEXT is expanded.  Presumably TEXT contains references to that
  732. variable, so its expansion will be different each time.
  733.    The result is that TEXT is expanded as many times as there are
  734. whitespace-separated words in LIST.  The multiple expansions of TEXT
  735. are concatenated, with spaces between them, to make the result of
  736. `foreach'.
  737.    This simple example sets the variable `files' to the list of all
  738. files in the directories in the list `dirs':
  739.      dirs := a b c d
  740.      files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
  741.    Here TEXT is `$(wildcard $(dir)/*)'.  The first repetition finds the
  742. value `a' for `dir', so it produces the same result as `$(wildcard
  743. a/*)'; the second repetition produces the result of `$(wildcard b/*)';
  744. and the third, that of `$(wildcard c/*)'.
  745.    This example has the same result (except for setting `dirs') as the
  746. following example:
  747.      files := $(wildcard a/* b/* c/* d/*)
  748.    When TEXT is complicated, you can improve readability by giving it a
  749. name, with an additional variable:
  750.      find_files = $(wildcard $(dir)/*)
  751.      dirs := a b c d
  752.      files := $(foreach dir,$(dirs),$(find_files))
  753. Here we use the variable `find_files' this way.  We use plain `=' to
  754. define a recursively-expanding variable, so that its value contains an
  755. actual function call to be reexpanded under the control of `foreach'; a
  756. simply-expanded variable would not do, since `wildcard' would be called
  757. only once at the time of defining `find_files'.
  758.    The `foreach' function has no permanent effect on the variable VAR;
  759. its value and flavor after the `foreach' function call are the same as
  760. they were beforehand.  The other values which are taken from LIST are
  761. in effect only temporarily, during the execution of `foreach'.  The
  762. variable VAR is a simply-expanded variable during the execution of
  763. `foreach'.  If VAR was undefined before the `foreach' function call, it
  764. is undefined after the call.  *Note The Two Flavors of Variables:
  765. Flavors.
  766.    You must take care when using complex variable expressions that
  767. result in variable names because many strange things are valid variable
  768. names, but are probably not what you intended.  For example,
  769.      files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
  770. might be useful if the value of `find_files' references the variable
  771. whose name is `Esta escrito en espanol!' (es un nombre bastante largo,
  772. no?), but it is more likely to be a mistake.
  773. File: make.info,  Node: Origin Function,  Next: Shell Function,  Prev: Foreach Function,  Up: Functions
  774. The `origin' Function
  775. =====================
  776.    The `origin' function is unlike most other functions in that it does
  777. not operate on the values of variables; it tells you something *about*
  778. a variable.  Specifically, it tells you where it came from.
  779.    The syntax of the `origin' function is:
  780.      $(origin VARIABLE)
  781.    Note that VARIABLE is the *name* of a variable to inquire about; not
  782. a *reference* to that variable.  Therefore you would not normally use a
  783. `$' or parentheses when writing it.  (You can, however, use a variable
  784. reference in the name if you want the name not to be a constant.)
  785.    The result of this function is a string telling you how the variable
  786. VARIABLE was defined:
  787. `undefined'
  788.      if VARIABLE was never defined.
  789. `default'
  790.      if VARIABLE has a default definition, as is usual with `CC' and so
  791.      on.  *Note Variables Used by Implicit Rules: Implicit Variables.
  792.      Note that if you have redefined a default variable, the `origin'
  793.      function will return the origin of the later definition.
  794. `environment'
  795.      if VARIABLE was defined as an environment variable and the `-e'
  796.      option is *not* turned on (*note Summary of Options: Options
  797.      Summary.).
  798. `environment override'
  799.      if VARIABLE was defined as an environment variable and the `-e'
  800.      option *is* turned on (*note Summary of Options: Options Summary.).
  801. `file'
  802.      if VARIABLE was defined in a makefile.
  803. `command line'
  804.      if VARIABLE was defined on the command line.
  805. `override'
  806.      if VARIABLE was defined with an `override' directive in a makefile
  807.      (*note The `override' Directive: Override Directive.).
  808. `automatic'
  809.      if VARIABLE is an automatic variable defined for the execution of
  810.      the commands for each rule (*note Automatic Variables: Automatic.).
  811.    This information is primarily useful (other than for your curiosity)
  812. to determine if you want to believe the value of a variable.  For
  813. example, suppose you have a makefile `foo' that includes another
  814. makefile `bar'.  You want a variable `bletch' to be defined in `bar' if
  815. you run the command `make -f bar', even if the environment contains a
  816. definition of `bletch'.  However, if `foo' defined `bletch' before
  817. including `bar', you do not want to override that definition.  This
  818. could be done by using an `override' directive in `foo', giving that
  819. definition precedence over the later definition in `bar';
  820. unfortunately, the `override' directive would also override any command
  821. line definitions.  So, `bar' could include:
  822.      ifdef bletch
  823.      ifeq "$(origin bletch)" "environment"
  824.      bletch = barf, gag, etc.
  825.      endif
  826.      endif
  827. If `bletch' has been defined from the environment, this will redefine
  828.    If you want to override a previous definition of `bletch' if it came
  829. from the environment, even under `-e', you could instead write:
  830.      ifneq "$(findstring environment,$(origin bletch))" ""
  831.      bletch = barf, gag, etc.
  832.      endif
  833.    Here the redefinition takes place if `$(origin bletch)' returns
  834. either `environment' or `environment override'.  *Note Functions for
  835. String Substitution and Analysis: Text Functions.
  836. File: make.info,  Node: Shell Function,  Prev: Origin Function,  Up: Functions
  837. The `shell' Function
  838. ====================
  839.    The `shell' function is unlike any other function except the
  840. `wildcard' function (*note The Function `wildcard': Wildcard Function.)
  841. in that it communicates with the world outside of `make'.
  842.    The `shell' function performs the same function that backquotes
  843. (``') perform in most shells: it does "command expansion".  This means
  844. that it takes an argument that is a shell command and returns the
  845. output of the command.  The only processing `make' does on the result,
  846. before substituting it into the surrounding text, is to convert each
  847. newline or carriage-return / newline pair to a single space.  It also
  848. removes the trailing (carriage-return and) newline, if it's the last
  849. thing in the result.
  850.    The commands run by calls to the `shell' function are run when the
  851. function calls are expanded.  In most cases, this is when the makefile
  852. is read in.  The exception is that function calls in the commands of
  853. the rules are expanded when the commands are run, and this applies to
  854. `shell' function calls like all others.
  855.    Here are some examples of the use of the `shell' function:
  856.      contents := $(shell cat foo)
  857. sets `contents' to the contents of the file `foo', with a space (rather
  858. than a newline) separating each line.
  859.      files := $(shell echo *.c)
  860. sets `files' to the expansion of `*.c'.  Unless `make' is using a very
  861. strange shell, this has the same result as `$(wildcard *.c)'.
  862. File: make.info,  Node: Running,  Next: Implicit Rules,  Prev: Functions,  Up: Top
  863. How to Run `make'
  864. *****************
  865.    A makefile that says how to recompile a program can be used in more
  866. than one way.  The simplest use is to recompile every file that is out
  867. of date.  Usually, makefiles are written so that if you run `make' with
  868. no arguments, it does just that.
  869.    But you might want to update only some of the files; you might want
  870. to use a different compiler or different compiler options; you might
  871. want just to find out which files are out of date without changing them.
  872.    By giving arguments when you run `make', you can do any of these
  873. things and many others.
  874.    The exit status of `make' is always one of three values:
  875.      The exit status is zero if `make' is successful.
  876.      The exit status is two if `make' encounters any errors.  It will
  877.      print messages describing the particular errors.
  878.      The exit status is one if you use the `-q' flag and `make'
  879.      determines that some target is not already up to date.  *Note
  880.      Instead of Executing the Commands: Instead of Execution.
  881. * Menu:
  882. * Makefile Arguments::          How to specify which makefile to use.
  883. * Goals::                       How to use goal arguments to specify which
  884.                                   parts of the makefile to use.
  885. * Instead of Execution::        How to use mode flags to specify what
  886.                                   kind of thing to do with the commands
  887.                                   in the makefile other than simply
  888.                                   execute them.
  889. * Avoiding Compilation::        How to avoid recompiling certain files.
  890. * Overriding::                  How to override a variable to specify
  891.                                   an alternate compiler and other things.
  892. * Testing::                     How to proceed past some errors, to
  893.                                   test compilation.
  894. * Options Summary::             Summary of Options
  895. File: make.info,  Node: Makefile Arguments,  Next: Goals,  Up: Running
  896. Arguments to Specify the Makefile
  897. =================================
  898.    The way to specify the name of the makefile is with the `-f' or
  899. `--file' option (`--makefile' also works).  For example, `-f altmake'
  900. says to use the file `altmake' as the makefile.
  901.    If you use the `-f' flag several times and follow each `-f' with an
  902. argument, all the specified files are used jointly as makefiles.
  903.    If you do not use the `-f' or `--file' flag, the default is to try
  904. `GNUmakefile', `makefile', and `Makefile', in that order, and use the
  905. first of these three which exists or can be made (*note Writing
  906. Makefiles: Makefiles.).
  907.